import 'package:ccshop/entites/GoodsEntity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class HomeMenuListView extends StatefulWidget {
  @override
  _HomeMenuListViewState createState() => _HomeMenuListViewState();
}

class _HomeMenuListViewState extends State<HomeMenuListView> {
  int _currentIndex = 0;
  List<String> tabList;

  List<GoodsEntity> goodsList = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tabList = List();
    tabList
      ..add("猜你喜欢")
      ..add("限量特价")
      ..add("蔬菜豆制品")
      ..add("新鲜水果")
      ..add("肉禽蛋")
      ..add("海鲜水产")
      ..add("熟食凉菜")
      ..add("速冻食品");

    GoodsEntity goodsEntity = GoodsEntity(
      "images/test/test01.png",
      "波兰土豆进口【次日达】",
      "波兰原装进口 新鲜自然 醇香浓厚",
      "￥39.99",
      "￥45.00",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test02.png",
      "瑞士大白菜进口【今日达】",
      "波兰原装进口 清甜可口",
      "￥9.99",
      "￥25.00",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test03.png",
      "粉西红柿（番茄）500g/斤",
      "口味甘甜 圆润可口 皮薄肉多",
      "11.99",
      "￥35.00",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test04.png",
      "新鲜的猪肉 500g/斤",
      "每日新鲜 赶快来买",
      "25.99",
      "￥35.05",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test05.png",
      "刚出的竹笋",
      "大熊猫和人都喜欢吃的",
      "29.99",
      "45.15",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test06.png",
      "超新鲜蒜台 450g/斤",
      "甘甜可口 蒜台炒肉",
      "9.99",
      "18.99",
    );
    goodsList.add(goodsEntity);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 10, right: 10),
      height: 50,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          shrinkWrap: true,
          itemCount: tabList.length,
          itemBuilder: (context, index) {
            return GestureDetector(
              onTap: () {
                setState(() {
                  _currentIndex = index;
                });
              },
              child: Container(
                padding: EdgeInsets.only(left: 10, right: 10),
                child: Column(
                  children: [
                    Text(
                      tabList[index],
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: _currentIndex == index ? Colors.red : Colors.black
                      ),
                    ),
                    _currentIndex == index
                        ? Container(
                            width: 50,
                            height: 3,
                            margin: EdgeInsets.only(top: 10),
                            color: Colors.red,
                          )
                        : Container(
                            width: 50,
                            height: 3,
                            margin: EdgeInsets.only(top: 10),
                            color: Colors.transparent,
                          )
                  ],
                ),
              ),
            );
          }),
    );
  }
}
